package com.atguigu.jxc.controller;

import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    @PostMapping("/list")
    public String list(HttpServletRequest request){
        Integer page = Integer.valueOf(request.getParameter("page"))-1;
        Integer rows = Integer.valueOf(request.getParameter("rows"));
        String supplierName = request.getParameter("supplierName");
        List<Supplier> list = supplierService.getList(page,rows,supplierName);
        int size = list.size();
        Map<String,Object> map = new HashMap<>();
        map.put("total",size);
        map.put("rows",list);
        Gson gson = new Gson();
        String result = gson.toJson(map);
        return result;
    }

    @RequestMapping("/save")
    public String save(Supplier supplier,@RequestParam(required = false)String supplierId){
        return supplierService.save(supplier, supplierId);
    }
    @PostMapping("delete")
    public String delete(@RequestParam(value = "ids")String[] ids){
        //System.out.println("hhhhh");
        return supplierService.delete(ids);
    }
}
